package com.ky.manage.activity.emergency;

import android.content.Intent;
import android.text.TextUtils;
import android.widget.TextView;

import com.ky.manage.R;
import com.ky.manage.base.BaseActivity;
import com.ky.manage.model.response.EmergencyDetailInfoResp;
import com.ky.manage.utils.ToastUtils;
import com.ky.manage.utils.ZyUtils;
import com.ky.manage.utils.http.AsyncHttpUtils;
import com.ky.manage.utils.http.BusinessHttpReqUtils;
import com.loopj.android.http.TextHttpResponseHandler;
import com.scwang.smart.refresh.footer.ClassicsFooter;
import com.scwang.smart.refresh.header.ClassicsHeader;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.zzhoujay.richtext.RichText;

import cz.msebera.android.httpclient.Header;

import static com.ky.manage.constant.OverallData.Emergency_Pei_Xun;
import static com.ky.manage.constant.OverallData.Emergency_Shi_Gu_Deal;
import static com.ky.manage.constant.OverallData.Emergency_Xuan_Chuan;
import static com.ky.manage.constant.OverallData.Emergency_Yan_Lian;

/**
 * 应急详情
 */
public class EmergencyDetailActivity extends BaseActivity {
    private String id = "", mServiceApi = "", mPageTitle = "详情页";
    private int type;
    private RefreshLayout refreshLayout;
    private TextView titleTv, descTv;

    public static void toEmergencyDetailActivity(BaseActivity context, String id, int type) {
        Intent intent = new Intent(context, EmergencyDetailActivity.class);
        intent.putExtra("id", id);
        intent.putExtra("type", type);
        context.toActivity(intent);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_emergency_detail;
    }

    @Override
    protected void initView() {
        findView(R.id.back_iv, v -> {
            finish();
        });
        titleTv = findView(R.id.title_tv);
        descTv = findView(R.id.desc_tv);
    }

    @Override
    protected void initData() {
        id = getIntent().getStringExtra("id");
        type = getIntent().getIntExtra("type", Emergency_Pei_Xun);
        initGlobalData();
        findTextView(R.id.page_title_tv, mPageTitle);
        refreshLayout = findView(R.id.refreshLayout);
        refreshLayout.autoRefresh();
        refreshLayout.setRefreshHeader(new ClassicsHeader(mCurActivity));
        refreshLayout.setRefreshFooter(new ClassicsFooter(mCurActivity));
        refreshLayout.setOnRefreshListener(refreshlayout -> {
            requestDetailData();
        });
    }

    private void initGlobalData() {
        switch (type) {
            case Emergency_Pei_Xun:
                mServiceApi = "/appApi/getTrainingInfo/" + id;
                mPageTitle = "应急培训详情";
                break;
            case Emergency_Shi_Gu_Deal:
                mServiceApi = "/appApi/getFaultInfo/" + id;
                mPageTitle = "事故处理详情";
                break;
            case Emergency_Xuan_Chuan:
                mServiceApi = "/appApi/getPublicizeInfo/" + id;
                mPageTitle = "应急宣传详情";
                break;
            case Emergency_Yan_Lian:
                mServiceApi = "/appApi/getDirllInfo/" + id;
                mPageTitle = "应急演练详情";
                break;
            default:
                break;
        }
    }

    private void requestDetailData() {
        ZyUtils.getInstance().showLoadingDialog(this, "");
        AsyncHttpUtils.getInstance().get(BusinessHttpReqUtils.SERVICE_DOMAIN + mServiceApi, new TextHttpResponseHandler() {
            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                ZyUtils.getInstance().insertLog("requestEmergencyDetail", "onFailure statusCode = "
                        + statusCode + ", responseString = " + responseString);
                ToastUtils.showRoundRectToast("加载失败");
                ZyUtils.getInstance().hideLoadingDialog();
                refreshLayout.finishRefresh();
            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, String responseString) {
                ZyUtils.getInstance().insertLog("requestEmergencyDetail", "onSuccess statusCode = "
                        + statusCode + ", responseString = " + responseString);
                ZyUtils.getInstance().hideLoadingDialog();
                refreshLayout.finishRefresh();
                if (statusCode == 200) {
                    EmergencyDetailInfoResp detailInfoResp = BusinessHttpReqUtils.getInstance().parseJsonObj(responseString, EmergencyDetailInfoResp.class);
                    if (null != detailInfoResp && detailInfoResp.code == 200 && null != detailInfoResp.data) {
                        titleTv.setText(getTitleContent(detailInfoResp.data));
                        RichText.fromHtml(getDescContent(detailInfoResp.data)).into(descTv);
                    } else {
                        ToastUtils.showRoundRectToast("加载失败");
                    }
                } else {
                    ToastUtils.showRoundRectToast("加载失败");
                }

            }
        });
    }

    private String getTitleContent(EmergencyDetailInfoResp.EmergencyDetailInfo data) {
        if (!TextUtils.isEmpty(data.publicizeTitle) && !"null".equals(data.publicizeTitle)) {
            return data.publicizeTitle;
        } else if (!TextUtils.isEmpty(data.title) && !"null".equals(data.title)) {
            return data.title;
        }
        return "";
    }

    private String getDescContent(EmergencyDetailInfoResp.EmergencyDetailInfo data) {
        if (!TextUtils.isEmpty(data.publicizeDesc) && !"null".equals(data.publicizeDesc)) {
            return data.publicizeDesc;
        } else if (!TextUtils.isEmpty(data.recordDesc) && !"null".equals(data.recordDesc)) {
            return data.recordDesc;
        } else if (!TextUtils.isEmpty(data.faultAnalyse) && !"null".equals(data.faultAnalyse)) {
            return data.faultAnalyse;
        }
        return "";
    }
}